[Proposal] Use 'Scratch Pad' instead of now.UnixNano
#156
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Using
now.UnixNano
will change<a>
's ID every time the site is built.In most cases, this is fine. But in my case, it didn't work well. When testing my site locally, a few
bs/collapse
shared the same ID. This made them to be opened or closed at once, together. I don't know why that happened, because that means multiplebs/collapse
were generated at the same time, and the chance is highly unlikely.So I changed the
$id
part to be like this:This also works for creating distinct IDs.
Both
now.UnixNano
anddelimit (shuffle (seq 1 9)) ""
have a problem. They will create a different ID every time they are called. If you build a site twice without changing anything, the build results won't be the same because of the different IDs.After a brief search, I've found that Hugo supports a per-page scratch pad. Since the scratch pad is only shared on a single page, we can safely use incremental numbers for the IDs because no same number will occur on a single page if we increment it properly.
I tested this for my site, and it works as intended. It even worked for nested
bs/collapse
. The only difference is that I use%03d
instead of%d
for numbers.This might increase build time because now Hugo has to assign a map for each page, instead of just getting
now.UnixNano
.